<?

class exchange_rates {


	var $filename = 'tasks/xe.xml';


// Function // Get exchange rate  ===============================================
function get_exchange_rate($symbol,$gbp_commission=''){



	$sql = "SELECT rate 
	FROM exchange_rates
	WHERE currency = '".$symbol."'
	LIMIT 1 ";
	
	$result = mysql_query($sql) or die (mysql_error());
	
	$row = mysql_fetch_array($result);
	
	if(($gbp_commission) AND ($row['currency'] == 'GBP')){
		
		$actual_rate = $row['rate'];
		$row['rate'] = $actual_rate - ($actual_rate * 0.04);
	
	}

	return $row['rate'];

}
// END FUNCTION ================================================================


// Function // Get exchange rate  ===============================================
function get_all_rates($named=''){

	if($named) $where = " WHERE name != '' ";



	$sql = "SELECT * 
	FROM exchange_rates
	".$where."
	ORDER BY currency ";
	
	$result = mysql_query($sql) or die (mysql_error());
	$values = array();
	
	while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
	
		$values[] = $row;
		
	}
		
	return $values;

}
// END FUNCTION


// Function // Get exchange rates from ECB
function download_exchange_rates(){

	$doc_remote = new DOMDocument();
	$doc_remote->load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
	
	$cube = $doc_remote->getElementsByTagName('Cube');
	
	if($cube){
	
		// Create XML documnet
		$doc_local = new DOMDocument('1.0');
		$doc_local->formatOutput = true;
		
		$root = $doc_local->createElement('xe');
		$root = $doc_local->appendChild($root);
		
		foreach($cube as $cube_node){
		
		if($cube_node->getAttribute('currency')){
		
			$data_present = true;
		
			$cube_element = $doc_local->createElement("cube");
			$root->appendChild($cube_element);
			
				$attr_currency = $doc_local->createAttribute('currency');
				$cube_element->appendChild($attr_currency);
		
					$attr_text = $doc_local->createTextNode($cube_node->getAttribute('currency'));
					$attr_currency->appendChild($attr_text); 
					
				$attr_rate = $doc_local->createAttribute('rate');
				$cube_element->appendChild($attr_rate);
		
					$attr_text = $doc_local->createTextNode($cube_node->getAttribute('rate'));
					$attr_rate->appendChild($attr_text); 
					
		}

		}	
		
		if($data_present){
			if($doc_local->save(SITE_ROOT_PATH.$this->filename)) return true;
		}
	}
	


}
// END FUNCTION


// Function // Update rates to DB
function update_exchange_rates(){

	$doc = new DOMDocument();
	$doc->load(SITE_ROOT_PATH.$this->filename);

	
	$cube = $doc->getElementsByTagName('cube');
	
	foreach($cube as $node) {
	
	
		if($node->getAttribute('currency')){
		
		
		
			$sql = "UPDATE exchange_rates
			SET rate = '".$node->getAttribute('rate')."'
			WHERE currency = '".$node->getAttribute('currency')."' ";
			
			$result = mysql_query($sql) or die (mysql_error());
		
		}
		
	}

}
// END FUNCTION


}
?>